package com.example.walkingtour.data;

import java.io.File;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;
import com.example.walkingtour.*;

@SuppressLint("HandlerLeak")
public class FileRenameService extends Service {
	private static final String TAG = "Copy Service";
	// private static final String START = "Start FileRenameService";
	// private static final String CANCEL = "Cancel";
	public static final String NOTIFY_INTENT = "NotifyIntent";
	private static int count = 0;
	private static Looper mServiceLooper;
	// private static ServiceHandler mServiceHandler;
	private static ServiceHandler mServiceHandler;
	public static final String OLD_FILE = "oldFile";
	public static final String NEW_FILE = "newFile";

	// Handler that actually does the work in the Service
	private final class ServiceHandler extends Handler {

		// static Service context;
		public ServiceHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			String newPath = b.getString(NEW_FILE);
			String oldPath = b.getString(OLD_FILE);
			File newFile = new File(newPath);
			File oldFile = new File(oldPath);
			try {
				WalkingTourHelper.renameFile(oldFile, newFile);
				/*
				 * newFile.createNewFile(); FileChannel input = (new
				 * FileInputStream (oldFile)).getChannel(); FileChannel output =
				 * (new FileOutputStream (newFile)).getChannel(); try {
				 * input.transferTo(0, input.size(), output); } finally { if
				 * (input != null) input.close(); if (output != null)
				 * output.close(); } oldFile.delete(); if(!newFile.exists() ||
				 * !newFile.canRead()) throw new
				 * Exception("File can not be read");
				 */

			} catch (Exception e) {
				Toast.makeText(getService(), "Error writing file",
						Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}

			// Since the job is done, stop the service here.
			stopSelf(msg.arg1);
		}
	}

	@Override
	/*
	 * This is where our service would notify the user that the alarm deadline
	 * has expired on their to-do list.
	 */
	public int onStartCommand(Intent intent, int flags, int startid) {
		Message msg = mServiceHandler.obtainMessage();
		msg.arg1 = count++;
		if (intent == null)
			return Service.START_NOT_STICKY;
		Bundle b = intent.getExtras();
		if (b == null)
			return Service.START_NOT_STICKY;
		msg.setData(intent.getExtras());

		mServiceHandler.sendMessage(msg);

		// If we get killed, after returning from here, restart
		return START_STICKY;
		// player.start();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Toast.makeText(this, "MyFileCopyService.onUnbind()", Toast.LENGTH_LONG)
				.show();
		Log.d(TAG, "onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		Toast.makeText(this, "MyFileCopyService.onBind()", Toast.LENGTH_LONG)
				.show();
		Log.d(TAG, "onBind");
		return null;
	}

	@Override
	public void onCreate() {
		HandlerThread thread = new HandlerThread("ServiceStartArgs",
				Process.THREAD_PRIORITY_BACKGROUND);
		thread.start();

		// Get the handler thread's looper and use it for our Handler
		mServiceLooper = thread.getLooper();
		mServiceHandler = new ServiceHandler(mServiceLooper);
		Toast.makeText(this, "MyFileService.onCreate()", Toast.LENGTH_LONG)
				.show();
		Log.d(TAG, "onCreate");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Toast.makeText(this, "MyFileService.onDestroy", Toast.LENGTH_LONG)
				.show();
		Log.d(TAG, "onDestroy");
	}

	public Service getService() {
		return this;
	}
}
